package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SyEmployeeServiceReservation;
import com.ruoyi.system.dto.SyEmployeeServiceReservationDTO;
import com.ruoyi.system.enumeration.SyTraditionalReservationEnum;
import com.ruoyi.system.mapper.SyEmployeeServiceReservationMapper;
import com.ruoyi.system.service.SyEmployeeServiceReservationService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.StoreIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SyEmployeeServiceReservationServiceImpl extends ServiceImpl<SyEmployeeServiceReservationMapper, SyEmployeeServiceReservation> implements SyEmployeeServiceReservationService {
    @Autowired
    private StoreIdUtils storeIdUtils;

    @Override
    public IPage<SyEmployeeServiceReservation> getAll(SyEmployeeServiceReservationDTO reservationDTO) {
        LambdaQueryWrapper<SyEmployeeServiceReservation> wrapper = Wrappers.lambdaQuery();
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getBookingVip())) {
            wrapper.eq(SyEmployeeServiceReservation::getBookingVip, reservationDTO.getBookingVip());
        }
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getAppointmentEmployees())) {
            wrapper.eq(SyEmployeeServiceReservation::getAppointmentEmployees, reservationDTO.getAppointmentEmployees());
        }
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getAppointmentProject())) {
            wrapper.eq(SyEmployeeServiceReservation::getAppointmentProject, reservationDTO.getAppointmentProject());
        }
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getOrderStatus())) {
            wrapper.eq(SyEmployeeServiceReservation::getOrderStatus, reservationDTO.getOrderStatus());
        }
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getStartTime()) && !AbdulEmptyUtils.isEmpty(reservationDTO.getEndTime())) {
            wrapper.between(SyEmployeeServiceReservation::getBookingTime, reservationDTO.getStartTime(), reservationDTO.getEndTime());
        }
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getArriveStartTime()) && !AbdulEmptyUtils.isEmpty(reservationDTO.getArriveEndTime())) {
            wrapper.between(SyEmployeeServiceReservation::getArrivalTime, reservationDTO.getArriveStartTime(), reservationDTO.getArriveEndTime());
        }
        if (!AbdulEmptyUtils.isEmpty(reservationDTO.getCancelStartTime()) && !AbdulEmptyUtils.isEmpty(reservationDTO.getCancelEndTime())) {
            wrapper.between(SyEmployeeServiceReservation::getAutomaticallyCancelTime, reservationDTO.getCancelStartTime(), reservationDTO.getCancelEndTime());
        }
        wrapper.eq(!AbdulEmptyUtils.isEmpty(reservationDTO.getBookingNumber()),SyEmployeeServiceReservation::getBookingNumber, reservationDTO.getBookingNumber());
        wrapper.eq(SyEmployeeServiceReservation::getStoreId, storeIdUtils.getStoreId());
        wrapper.orderByDesc(SyEmployeeServiceReservation::getUpdateTime);

        Page<SyEmployeeServiceReservation> page = this.page(new Page<>(reservationDTO.getPageNum(), reservationDTO.getPageSize()), wrapper);
        if (!AbdulEmptyUtils.isEmpty(page.getRecords())) {
            for (SyEmployeeServiceReservation syEmployeeServiceReservation : page.getRecords()) {
                if (!AbdulEmptyUtils.isEmpty(syEmployeeServiceReservation.getOrderStatus())) {
                    syEmployeeServiceReservation.setOrderStatusDesc(SyTraditionalReservationEnum.getValueByKey(syEmployeeServiceReservation.getOrderStatus()));
                }
            }
        }
        return page;
    }
}
